<template>
  <div class="app-container">
    <div class="filter-container">
      <el-form ref="filterForm" :model="filterForm" :inline="true" label-position="left" label-width="100px">
        <el-form-item label="成立时间" prop="establishDates">
          <el-date-picker 
          clearable
            v-model="filterForm.establishDates"
            value-format="yyyy-MM-dd"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="项目" prop="projectName">
          <el-input v-model="filterForm.projectName" clearable placeholder="项目编号/项目名称" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
        </el-form-item>
        <el-form-item label="投资金额" prop="investmentAmount">
          <el-col :span="11">
          <el-input v-model="projectForm.investmentAmount" clearable placeholder="最小金额"  />
          </el-col>
          <el-col class="line" :span="2">-</el-col>
          <el-col :span="11">
          <el-input v-model="projectForm.investmentAmount" clearable placeholder="最大金额"  />
          </el-col>
        </el-form-item>
        <el-form-item label="板块" prop="industryID">
            <el-select v-model="filterForm.industryID" clearable placeholder="请选择">
              <el-option label="0" value="0">全部</el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="状态" prop="status">
            <el-select v-model="filterForm.status" clearable placeholder="请选择">
              <el-option label="0" value="0">全部</el-option>
              <el-option label="6" value="6">已处理</el-option>
              <el-option label="6" value="5">待处理</el-option>
              <el-option label="1" value="1">处理中</el-option>
            </el-select>
        </el-form-item>
      </el-form>
      <el-button  @click="openSubscribeHidden =true">新项目</el-button>
      <el-button  @click="getList()">搜索</el-button>
    </div>
    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
      @sort-change="sortChange"
    >
    <el-table-column
     label="编号"
      type="selection"
      width="50">
    </el-table-column>
      <el-table-column label="主题">
        <template slot-scope="{row}">
          <div>{{ row.projectName }}</div>
          <div>{{ row.projectCode }}</div>
        </template>
      </el-table-column>
      <el-table-column label="行业" width="150px">
        <template slot-scope="{row}">
          <span>{{ row.industry.industryName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="资金" width="200px">
        <template slot-scope="{row}">
          <div>投资额：￥{{ row.investmentAmount }}</div>
          <div>头寸：￥{{ row.position }}</div>
          <div>利润：￥{{ row.profit }}</div>
        </template>
      </el-table-column>
      <el-table-column label="成立时间" width="150px">
        <template slot-scope="{row}">
          <span>{{ row.establishDate }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" width="150px">
        <template slot-scope="{row}">
          <el-tag v-if="row.projectStatus == 1" type="danger" effect="dark">立项</el-tag>
          <el-tag v-else-if="row.projectStatus == 2" type="warning" effect="dark">待处理</el-tag>
          <el-tag v-else-if="row.projectStatus == 3" type="info" effect="dark">运行中</el-tag>
          <el-tag v-else-if="row.projectStatus == 4" effect="dark">已结项</el-tag>
          <el-tag v-else effect="dark">未定义</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="230" class-name="small-padding fixed-width">
        <template slot-scope="{row}">
          <el-button type="primary" size="mini" @click="handleUpdate(row)">
            编辑
          </el-button>
          <el-button type="primary" size="mini" @click="handleShow(row)">
            详情
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="filterForm.page" :limit.sync="filterForm.size" @pagination="getList" />

    <el-dialog
      title="项目档案"
      :visible.sync="openSubscribeHidden"
      :close-on-click-modal="false"
      width="30%">
      <el-form ref="projectForm" :model="projectForm"  label-position="left" label-width="100px">
        <el-form-item label="项目名称" prop="projectName">
          <el-input v-model="projectForm.projectName" clearable placeholder="项目名称" style="width: 200px;" class="filter-item"/>
        </el-form-item>
        <el-form-item label="隶属板块" prop="industryID">
           <el-select v-model="projectForm.firstIndustryID" placeholder="请选择">
            <el-option
              v-for="item in industryOptions"
              :key="item.value"
              :label="item.industryName"
              :value="item.ID">
            </el-option>
          </el-select>
          <el-select v-model="projectForm.industryID" placeholder="请选择">
            <el-option
              v-for="item in subIndustryOptions"
              :key="item.value"
              :label="item.industryName"
              :value="item.ID">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="成立日期" prop="establishDate">
          <el-date-picker
            v-model="projectForm.establishDate"
            value-format="yyyy-MM-dd"
            type="date"
            placeholder="选择日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="投资金额" prop="investmentAmount">
          <el-input v-model="projectForm.investmentAmount" clearable placeholder="投资金额" style="width: 200px;" class="filter-item"/>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="projectForm.remark" clearable placeholder="备注" style="width: 200px;" class="filter-item"/>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="openSubscribeHidden = false">取 消</el-button>
        <el-button type="primary" @click="onSubjectDataSubmit()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
//import { getFaultinessList,  removeFaultinessByID, changeFaultinessStatusByID } from '@/api/development/faultiness'
import waves from '@/directive/waves' // waves directive
import { parseTime } from '@/utils'
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
import {getHost} from '@/utils/socket' // secondary package based on el-pagination

import {getList,saveProject} from '@/api/finance/capital'
import {getIndustryUsable} from '@/api/finance/industry'

export default {
  name: 'faultinessAdmin',
  components: { Pagination },
  directives: { waves },
  filters: {
  },
  data() {
    return {
      socket:"",
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: false,
      connected:"loading",
      openSubscribeHidden:false,
      industryOptions:[],
      subIndustryOptions:[],
      allowedRefreshSubIndustry:false,
      feelingsKeywordList:[{name:''}],
      projectForm:{
      },
      filterForm: {
        page: 1,
        size: 10,
        title: undefined,
        sort: '+id'
      }
    }
  },
  created() {
    this.getList()
    this.queryIndustryByIndustryID(0)
  },
  
  watch:{
    'projectForm.firstIndustryID'(industryID){
      this.subIndustryOptions = []
      this.allowedRefreshSubIndustry = true
      this.queryIndustryByIndustryID(industryID)
    }
  },
  methods: {
    onSubjectDataSubmit:function(){
      let _this = this
      saveProject(this.projectForm).then((response) => {
          if (response.code === 200) {
            _this.openSubscribeHidden = false
            _this.getList()
          } else {
            this.$message({
              message: response.msg,
              type: 'error'
            })
          }
      })
    },
    getList:function(){
      let _this = this;
      console.log(_this.filterForm)
      getList(_this.filterForm).then((response) => {
        console.log(response)
          if (response.code === 200) {
            this.list = response.data.list
            this.total = response.data.total
          } else {
            _this.$message({
              message: response.msg,
              type: 'error'
            })
          }
      })
    },
    handleShow:function(item){
      this.$router.push({ path: 'capital-detail', query: { 'capitalID': item.ID }})
    },
    queryIndustryByIndustryID:function(IndustryID){
      let _this = this
      let filterForm = {

      }
      IndustryID = parseInt(IndustryID)
      if(isNaN(IndustryID)){
        IndustryID = 0
      }
      filterForm['industryID'] = IndustryID
      console.log(filterForm)
      getIndustryUsable(filterForm).then((response) => {
          if (response.code === 200) {
            if(IndustryID){
              _this.subIndustryOptions = response.data.list
            }else{
            _this.industryOptions = response.data.list
            }
          } else {
            _this.$message({
              message: response.msg,
              type: 'error'
            })
          }
      })
    },
    handleUpdate:function(projectData){
      this.openSubscribeHidden = true
      this.projectForm = projectData
      this.projectForm['projectID'] = this.projectForm.ID
    },
    handleFilter() {
      this.filterForm.page = 1
      this.getList()
    },
    sortChange(data) {
      const { prop, order } = data
      if (prop === 'id') {
        this.sortByID(order)
      }
    },
    sortByID(order) {
      if (order === 'ascending') {
        this.filterForm.sort = '+id'
      } else {
        this.filterForm.sort = '-id'
      }
      this.handleFilter()
    }
  }
}
</script>
<style>

</style>